package edu.neu.cs6515.xml;

import java.io.InputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.apache.commons.io.IOUtils;

/**
 * Class to turn a valid XML String {@link Board} into a Board instance
 * containing the values of the xml doc
 */
public final class XMLPlacementSequenceUnmarshaller {

    private final JAXBContext context;
    private final Unmarshaller unmarshaller;

    /**
     * Initalizes unmarshaller
     */
    public XMLPlacementSequenceUnmarshaller() throws JAXBException {
        context = JAXBContext.newInstance(Board.class);
        unmarshaller = context.createUnmarshaller();
    }

    /**
     * Turns XML string into new Board object containing xml values, if xml does
     * not match the format {@link Board} an empty Board obj is returned
     * 
     * @param input
     *            xml string
     * @return new Seq object
     */
    public Board unmarshall(final String input) throws JAXBException {
        final InputStream inputStream = IOUtils.toInputStream(input);
        return (Board) unmarshaller.unmarshal(inputStream);
    }
}
